package com.iflytek.jcy.supervise.clue.web;

import com.iflytek.jcy.supervise.clue.common.pojo.CommonResponse;
import com.iflytek.jcy.supervise.clue.entity.TaskEntity;
import com.iflytek.jcy.supervise.clue.entity.TaskManageListBo;
import com.iflytek.jcy.supervise.clue.service.IClueService;
import com.iflytek.jcy.supervise.clue.service.TaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** <br>
 * 标题:任务相关 <br>
 * 描述: <br>
 * 公司: www.iflytek.com<br>
 * 
 * @autho dgyu
 * @time 2022年10月19日 下午4:19:21 */
@RestController
@Api(value = "任务接口", tags = { "任务接口" })
@RequestMapping("/task")
public class TaskController {

	protected final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	TaskService			   taskService;
	@Autowired
	IClueService		   iClueService;
	@ApiOperation(value = "新增任务接口", notes = "新增任务接口")
	@PostMapping(value = "/addTask")
	public CommonResponse addTask(@RequestBody TaskEntity taskEntit, HttpServletRequest request) {
		logger.info(taskEntit.toString());
		return taskService.addTask(taskEntit, request);
	}

	@ApiOperation(value = "查询任务管理列表接口", notes = "查询任务管理列表接口")
	@PostMapping(value = "/queryTaskManageList")
	// @SaCheckPermission("/task/queryTaskManageList")
	public CommonResponse queryTaskManageList(@RequestBody TaskManageListBo taskManageListBo) {
		return CommonResponse.success(taskService.queryTaskManageList(taskManageListBo));
	}

	@ApiOperation(value = "查询任务接口", notes = "查询任务接口")
	@PostMapping(value = "/queryTaskById")
	public CommonResponse queryTaskById(@RequestBody TaskEntity taskEntit) {
		return taskService.queryTaskById(taskEntit);
	}

	@ApiOperation(value = "修改任务接口", notes = "修改任务接口")
	@PostMapping(value = "/updateTask")
	public CommonResponse updateTask(@RequestBody TaskEntity taskEntit) {
		return taskService.updateTask(taskEntit);
	}

	@ApiOperation(value = "删除任务接口", notes = "删除任务接口")
	@PostMapping(value = "/deleteTask")
	public CommonResponse deleteTask(@RequestBody TaskEntity taskEntit) {
		return taskService.deleteTask(taskEntit);
	}

	@ApiOperation(value = "根据任务id查询任务列表接口", notes = "根据任务id查询任务列表接口")
	@PostMapping(value = "/queryTaskLogListByID")
	public CommonResponse queryTaskLogListByID(@RequestBody TaskManageListBo taskManageListBo) {
		return CommonResponse.success(taskService.queryTaskLogListByID(taskManageListBo, false));
	}

	@ApiOperation(value = "查询任务详情列表接口", notes = "查询任务详情列表接口")
	@PostMapping(value = "/queryTaskDetailList")
	public CommonResponse queryTaskDetailList(@RequestBody TaskManageListBo taskManageListBo) {
		return CommonResponse.success(taskService.queryTaskDetailList(taskManageListBo));
	}

	@ApiOperation(value = "中止任务", notes = "中止任务")
	@GetMapping(value = "/mykill/{xxlLogId}")
	public CommonResponse mykill(@PathVariable(required = true, name = "xxlLogId") Integer xxlLogId) {
		return taskService.mykill(xxlLogId);
	}

	@ApiOperation(value = "任务详情页下载(右上角下载按钮)", notes = "任务详情页下载(右上角下载按钮)")
	@RequestMapping(value = "/exportTaskLogDetail", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public void exportTaskLogDetail(@RequestBody TaskManageListBo taskManageListBo, HttpServletResponse response) throws Exception {
		long start = System.currentTimeMillis();
		logger.debug("任务详情页下载 start taskLogId:{}", taskManageListBo.getId());
		this.iClueService.exportTaskLogDetail(taskManageListBo, response);
		logger.debug("任务详情页下载 end！taskLogId:{}耗时：{}ms.", taskManageListBo.getId(), System.currentTimeMillis() - start);
	}
}
